<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同编号">
              <a-input placeholder="请输入合同编号" v-model="queryParam.contractNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="合同名称">
              <a-input placeholder="请输入合同名称" v-model="queryParam.contractName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户">
                <a-input placeholder="请输入客户名称" v-model="queryParam.customerName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="生效日期">
                <j-date placeholder="请选择生效日期" v-model="queryParam.effectDate"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="合同状态">
                <j-dict-select-tag placeholder="请选择合同状态" v-model="queryParam.status" dictCode="crm_contract_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'saleContract:add'" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('销售合同')"  v-has="'saleContract:exp'" >导出</a-button>
      <a-upload name="file"  v-has="'saleContract:imp'" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"  v-has="'saleContract:delBatch'" ><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <span v-has="'saleContract:effect'" >
            <a-divider type="vertical" />
            <a-popconfirm v-if="record.status === '01'" title="确定生效吗?" @confirm="() => handleEffect(record)">
              <a>生效</a>
            </a-popconfirm>
            <span v-else style="color: #C0C0C0">生效</span>
          </span>
          <span v-has="'saleContract:edit'" >
            <a-divider type="vertical" />
            <a v-if="record.status === '01'" @click="handleEdit(record)">编辑</a>
            <span v-else style="color: #C0C0C0">编辑</span>
          </span>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-has="'saleContract:print'">
                <a @click="handlePrint(record)">打印</a>
              </a-menu-item>
              <a-menu-item v-has="'saleContract:invalid'">
                <a-popconfirm v-if="record.status ==='02' || record.status ==='03' " title="确定作废吗?" @confirm="() => handleInvalid(record)">
                  <a>作废</a>
                </a-popconfirm>
                <span v-else style="color: #C0C0C0">作废</span>
              </a-menu-item>
              <a-menu-item v-has="'saleContract:del'">
                <a-popconfirm v-if="record.status === '01'" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
                <span v-else style="color: #C0C0C0">删除</span>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <sd-contract-modal ref="modalForm" @ok="modalFormOk"/>
    <yq-print-modal ref="modalPrint" report-id="817758517358014464" :modal-width="1200"/>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import SdContractModal from './modules/SdContractModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import '@/assets/less/TableExpand.less'
  import {putAction} from "@/api/manage";
  import {curDate} from "@/utils/util";
  import YqPrintModal from "@/views/slf/components/print/YqPrintModal";

  export default {
    name: "SdContractList",
    mixins:[JeecgListMixin],
    components: {
      SdContractModal,
      YqPrintModal
    },
    data () {
      return {
        description: '销售合同管理页面',
        // 表头
        columns: [
          {
            title:'合同编号',
            align:"center",
            dataIndex: 'contractNo'
          },
          {
            title:'客户',
            align:"center",
            dataIndex: 'customerName'
          },
          {
            title:'项目',
            align:"center",
            dataIndex: 'projectId_dictText'
          },
          {
            title:'合同金额',
            align:"center",
            dataIndex: 'money'
          },
          {
            title:'销售员',
            align:"center",
            dataIndex: 'salesman_dictText'
          },
          {
            title:'销售部门',
            align:"center",
            dataIndex: 'salesDept_dictText'
          },
          {
            title:'纸质合同',
            align:"center",
            dataIndex: 'paperNo'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'签约日期',
            align:"center",
            dataIndex: 'signDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'生效日期',
            align:"center",
            dataIndex: 'effectDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'合同名称',
            align:"center",
            dataIndex: 'contractName'
          },
          {
            title:'合同类型',
            align:"center",
            dataIndex: 'contractType_dictText'
          },
          {
            title:'销售类型',
            align:"center",
            dataIndex: 'saleType_dictText'
          },
          {
            title:'截至日期',
            align:"center",
            dataIndex: 'expireDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'负责人',
            align:"center",
            dataIndex: 'owner_dictText'
          },
          {
            title:'负责部门',
            align:"center",
            dataIndex: 'ownerDept_dictText'
          },
          {
            title:'币种',
            align:"center",
            dataIndex: 'currencyNo_dictText'
          },
          {
            title:'签署单位',
            align:"center",
            dataIndex: 'customerSign_dictText'
          },
          {
            title:'客户签约人',
            align:"center",
            dataIndex: 'customerMan'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/sd/contract/list",
          delete: "/sd/contract/delete",
          modifyContract: "/sd/contract/modifyContract",
          deleteBatch: "/sd/contract/deleteBatch",
          exportXlsUrl: "/sd/contract/exportXls",
          importExcelUrl: "sd/contract/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
         fieldList.push({type:'string',value:'contractNo',text:'合同编号',dictCode:''})
         fieldList.push({type:'string',value:'contractName',text:'合同名称',dictCode:''})
         fieldList.push({type:'popup',value:'customerId',text:'客户', popup:{code:'crm_customer_pop',field:'id',orgFields:'id',destFields:'customerId'}})
         fieldList.push({type:'string',value:'customerName',text:'客户名称',dictCode:''})
         fieldList.push({type:'string',value:'contractType',text:'合同类型',dictCode:'crm_contract_type'})
         fieldList.push({type:'string',value:'saleType',text:'销售类型',dictCode:'crm_contract_sale_type'})
         fieldList.push({type:'string',value:'paperNo',text:'纸质合同',dictCode:''})
         fieldList.push({type:'BigDecimal',value:'money',text:'合同金额',dictCode:''})
         fieldList.push({type:'date',value:'signDate',text:'签约日期'})
         fieldList.push({type:'date',value:'effectDate',text:'生效日期'})
         fieldList.push({type:'date',value:'expireDate',text:'截至日期'})
         fieldList.push({type:'string',value:'customerSign',text:'签署单位',dictCode:''})
         fieldList.push({type:'string',value:'customerMan',text:'客户签约人',dictCode:''})
         fieldList.push({type:'string',value:'status',text:'合同状态',dictCode:'crm_contract_status'})
         fieldList.push({type:'popup',value:'projectId',text:'项目', popup:{code:'quote_project_popup',field:'id',orgFields:'id',destFields:'project_id'}})
         fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
         fieldList.push({type:'sel_user',value:'salesman',text:'销售员'})
         fieldList.push({type:'sel_depart',value:'salesDept',text:'销售部门'})
         fieldList.push({type:'sel_user',value:'owner',text:'负责人'})
         fieldList.push({type:'sel_depart',value:'ownerDept',text:'负责部门'})
         fieldList.push({type:'string',value:'currencyNo',text:'币种',dictCode:''})
        this.superFieldList = fieldList
      },
      handleEffect(record){
        let that = this;
        putAction(that.url.modifyContract, {id: record.id, status: '02', effectDate: curDate()}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(1)
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handleInvalid(record){
        let that = this;
        putAction(that.url.modifyContract, {id: record.id, status: '09'}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(1)
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handlePrint(record){
        this.$refs.modalPrint.print(record.id);
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>